/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package csci6450.chickenFriedFS.nuggetServer;

import csci6450.chickenFriedFS.nuggetCore.Nugget;
import csci6450.chickenFriedFS.nuggetCore.NuggetConstants;
import csci6450.chickenFriedFS.nuggetCore.NuggetLog;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jacobschoen
 */
public class NuggetServerLog extends NuggetLog {


    private static NuggetServerLog log;

    static{
        log = new NuggetServerLog();
    }

    public static NuggetServerLog getInstance() {
        return log;
    }

    private NuggetServerLog(){
        try {
            this.setLogLocation(RuntimeSettings.getInstance().getNuggetServerLogLocation());
            this.init();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(NuggetServerLog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(NuggetServerLog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(NuggetServerLog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected String getLogString(Nugget nugget) {
        String temp = "";
        //the parent filename
        temp = temp + nugget.getParentFilename() + NuggetConstants.SEPARATOR;
        //the nuggets position in the file
        temp = temp + nugget.getNuggetPosition() + NuggetConstants.SEPARATOR;
        //the number of nuggets that make up the file
        temp = temp + nugget.getParentFileNuggetCount() + NuggetConstants.SEPARATOR;
        //the nugget identifier
        temp = temp + nugget.getNuggetIdentifier() + NuggetConstants.SEPARATOR;
        //the server location of the nugget
        temp = temp + nugget.getNuggetLocation() + NuggetConstants.SEPARATOR;
        //the offset to the nugget on the object server
        temp = temp + nugget.getNuggetOffset() + NuggetConstants.SEPARATOR;
        //the nugget BufferStart
        temp = temp + nugget.getBufferStart() + NuggetConstants.SEPARATOR;
        //the nugget replication ip
        temp = temp + nugget.getNuggetRepLocation() + NuggetConstants.SEPARATOR;
        //the nugget replication port
        temp = temp + nugget.getNuggetRepPort() + NuggetConstants.SEPARATOR;
        //the nugget replication port
        temp = temp + nugget.getRepNuggetOffset() + NuggetConstants.SEPARATOR;
        //the timestamp of when this nugget was created
        temp = temp + NuggetConstants.DATE_FORMATTER.format(nugget.getTimestamp()) + NuggetConstants.SEPARATOR;
        return temp;
    }

    @Override
    protected void processLine(String line) throws ParseException {
        StringTokenizer tokenizer = new StringTokenizer(line, NuggetConstants.SEPARATOR);
        String parentFileName = tokenizer.nextToken();
        String nuggetPosition = tokenizer.nextToken();
        String parentFileNuggetCount = tokenizer.nextToken();
        String nuggetIdentifier = tokenizer.nextToken();
        String nuggetLocation = tokenizer.nextToken();
        String nuggetOffset = tokenizer.nextToken();
        String bufferStart = tokenizer.nextToken();
        String repLocation = tokenizer.nextToken();
        String repPort = tokenizer.nextToken();
        String repOffset = tokenizer.nextToken();
        String timestamp = tokenizer.nextToken();
        String function = tokenizer.nextToken();

        //make nugget
        Nugget nugget = new Nugget(parentFileName, Integer.parseInt(nuggetPosition),
                Integer.parseInt(parentFileNuggetCount), nuggetIdentifier,
                nuggetLocation, Long.parseLong(nuggetOffset),
                NuggetConstants.DATE_FORMATTER.parse(timestamp), 
                RuntimeSettings.getInstance().getClientListenerPort(),
                Integer.parseInt(bufferStart));
        nugget.setNuggetRepLocation(repLocation);
        nugget.setNuggetRepPort(Integer.parseInt(repPort));
        nugget.setRepNuggetOffset(Long.parseLong(repOffset));
        if (function.equals(NuggetConstants.ADD)) {
            this.nuggetList.add(nugget);
            this.latestNuggets.add(nugget);
            NuggetFileStoreManager.getInstance().addNugget(nugget);
        } else {
            this.nuggetList.remove(nugget);
        }

    }

    


}
